package com.hearwego;

import java.util.List;

import com.jwetherell.augmented_reality.activity.AugmentedReality;
import com.jwetherell.augmented_reality.data.ARData;
import com.jwetherell.augmented_reality.data.LocalDataSource;
import com.utilities.Informacion;
import com.utilities.RssParserPull;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import android.util.Log;
import android.view.Menu;
import android.widget.SlidingDrawer;

public class ActivityAugmentedReality extends AugmentedReality {
	SharedPreferences preferences;
	SharedPreferences.Editor editor;
	String query, cat_cve;
	private ProgressDialog progressDialog;
	public List<Informacion> resultados, catpoi, caticon;
	public RssParserPull parser;
	String[] arraynombre, arraypoid, arrayicon, arraylat, arraylong, arraydireccion;
//	private static GoogleMap mMap;
//	private MarkerOptions markeropt = null;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_augmented_reality);
		preferences= getSharedPreferences("Preferencias",Context.MODE_PRIVATE);
		editor = preferences.edit();
		new LoadInfo().execute("");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_augmented_reality, menu);
		return true;
	}
	
	
	public class LoadInfo extends AsyncTask<String, String, String>{

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
    		Log.i("PRE", "PRE");
			progressDialog = ProgressDialog.show(ActivityAugmentedReality.this, "",
					"Cargando...", true, true, new OnCancelListener() {
						
						@Override
						public void onCancel(DialogInterface arg0) {
							onBackPressed();
						}
			});
		}	
		
		@Override
		protected String doInBackground(String... params) {
			Log.i("DO", "DO");

			parser = new RssParserPull(preferences.getString("actualquery", getString(R.string.query)));
			resultados = parser.parse();
			arraynombre = new String[resultados.size()];
			arraypoid = new String[resultados.size()];
			arraylat = new String[resultados.size()];
			arraylong = new String[resultados.size()];
			arraydireccion = new String[resultados.size()];
			arrayicon = new String[resultados.size()];
			
			for(int i=0; i<resultados.size(); i++){
				Log.i("arraynombre", ""+resultados.get(i).getNombre());
				Log.i("arraypoid", ""+resultados.get(i).getId());
				Log.i("arraylat", ""+resultados.get(i).getLat());
				Log.i("arraylong", ""+resultados.get(i).getLng());
				Log.i("arrayicon", ""+resultados.get(i).getIcon());
				Log.i("arraydireccion", ""+resultados.get(i).getDireccion());


				arraynombre[i] = resultados.get(i).getNombre();
				arraypoid[i] = resultados.get(i).getId();
				arraylat[i] = resultados.get(i).getLat();
				arraylong[i] = resultados.get(i).getLng();
				arraydireccion[i] = resultados.get(i).getDireccion();
				parser = new RssParserPull("http://www.smartthinking.com.mx:8080/HearWeGo/DataServlet?" +
						"Query=select+cat_cve+from+pois_categorias+where+poi_idr="+arraypoid[i]+"&BaseDatos=hwg");
				Log.i("cat_eve url", "http://www.smartthinking.com.mx:8080/HearWeGo/DataServlet?" +
						"Query=select+cat_cve+from+pois_categorias+where+poi_idr="+arraypoid[i]+"&BaseDatos=hwg");
				catpoi = parser.parse();
				Log.i("SIZE", catpoi.size()+"");
				if(catpoi.size() == 0){
					cat_cve = "6";
				}else{
					cat_cve = catpoi.get(0).getClave();
				}
				
				
				Log.i("cat_cve", cat_cve+"");

				parser = new RssParserPull("http://www.smartthinking.com.mx:8080/HearWeGo/DataServlet?" +
						"Query=select+cat_icon+from+categorias+where+cat_cve="+cat_cve+"&BaseDatos=hwg");
				Log.i("cat_icon", "http://www.smartthinking.com.mx:8080/HearWeGo/DataServlet?" +
						"Query=select+cat_icon+from+categorias+where+cat_cve="+cat_cve+"&BaseDatos=hwg");
				caticon = parser.parse();
				if(caticon.size() == 0){
					arrayicon[i] = "1";
				}else{
					arrayicon[i] = caticon.get(0).getIcon();
				}
				
				
			
				
			}
			
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
    		Log.i("POST", "POST");
    		// Local
            LocalDataSource localData = new LocalDataSource(getApplicationContext().getResources());
            ARData.addMarkers(localData.getMarkers(arraynombre, arraylat, arraylong, arrayicon));
    		
    		
			progressDialog.dismiss();
		}
		
		
	}

}
